package site.xuxingcan.messageclient;

import java.io.*;
import java.net.Socket;

/**
 * Created by mengniu on 16-6-27.
 */
public class SocketHelper {
    private BufferedWriter os;
    private BufferedReader is;
    private Socket socket;

    public SocketHelper(Socket socket) throws IOException {
        this.socket = socket;
        os = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF-8"));
        is = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));
    }

    public void writeLineAndFlush(String s) throws IOException {
        if (!s.endsWith("\n")) {
            s = s + "\n";
        }
        os.write(s);
        os.flush();
    }

    public String readLine() throws IOException {
        return is.readLine();
    }

    public void close() {
        try {
            socket.close();
        } catch (Exception e) {

        }
    }
}